package com.larsonzhong.virtualcontrol;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.larsonzhong.virtualcontrol.RockerView;

import static com.larsonzhong.virtualcontrol.RockerView.DirectionMode.DIRECTION_8;

/**
 * Create by larsonzhong@163.com
 */
public class MainActivity extends AppCompatActivity {

    private RockerView mRockerView;
    private TextView mTvShake;
    private TextView mTvAngle;
    private TextView mTvLevel;
    private TextView mTvModel;
    int route = 270;
    int radius = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRockerView = (RockerView) findViewById(R.id.my_rocker);
        mTvAngle = (TextView) findViewById(R.id.tv_now_angle);
        mTvLevel = (TextView) findViewById(R.id.tv_now_level);
        mTvModel = (TextView) findViewById(R.id.tv_now_model);
        mTvShake = (TextView) findViewById(R.id.tv_now_shake);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        URL url = new URL("http://192.168.4.1/speed/" + radius + "/routation/" + route);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("GET");//设置请求方式为POST
                        connection.setDoOutput(true);//允许写出
                        connection.setDoInput(true);//允许读入
                        connection.setUseCaches(false);//不使用缓存
                        connection.setReadTimeout(1000);
                        Log.i("kwwl", "before");
                        connection.connect();//连接
                        int responseCode = connection.getResponseCode();
                        if (responseCode == HttpURLConnection.HTTP_OK) {
                            InputStream inputStream = connection.getInputStream();
                            inputStream.close();
                        }
                        connection.disconnect();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        mRockerView.setOnAngleChangeListener(new RockerView.OnAngleChangeListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void angle(double angle) {
                mTvAngle.setText("当前角度："+angle);
                route=(int)angle;
            }

            @Override
            public void onFinish() {

            }
        });

        mRockerView.setOnDistanceLevelListener(new RockerView.OnDistanceLevelListener() {
            @Override
            public void onDistanceLevel(int level) {
                radius = level;
                mTvLevel.setText("当前距离级别："+level);
            }
        });
    }
}
